//
//  JSCheckBoxCell.swift
//  fho
//
//  Created by 郭颢源 on 2021/3/25.
//

import UIKit

class JSCheckBoxCell: JSBoxViewCell {
    var selectedItemNames:NSMutableArray = NSMutableArray()
//    let kItemHeight:CGFloat = 40
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.layout.minimumLineSpacing = 0
        self.layout.minimumInteritemSpacing = 10
        self.collectionView.snp.makeConstraints { (make) in
            make.left.equalTo(self.cellMargin)
            make.right.equalTo(-self.cellMargin);
            make.top.equalTo(self.iconBtn.snp.bottom).offset(0);
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func initWith<T>(Model formModel: JSFormModel, VC ViewController: T) where T : BaseFormsController {
        super.initWith(Model: formModel, VC: ViewController)
        self.collectionView.snp.remakeConstraints { (make) in
            make.left.equalTo(self.cellMargin)
            make.right.equalTo(-self.cellMargin)
            make.top.equalTo(self.iconBtn.snp.bottom).offset(0)
            make.height.equalTo(getCollectionViewHeight())
        }
        
//        self.collectionView.reloadData()
    }
    
    func getCollectionViewHeight() -> CGFloat {
        
        var row:Int = 1 // 行数
        let contentSizeWidth:Float = Float(UIScreen.main.bounds.width - self.cellMargin * 2 - 40) //collectionSize width 区域， 40： uicollectionView 的左右边距
        var itemWidths:Float = 0.0
        for item in self.model.items {
            
            itemWidths += Float(item.width + self.layout.minimumInteritemSpacing)
            if itemWidths > contentSizeWidth {
                itemWidths = Float(item.width+self.layout.minimumInteritemSpacing)
                row+=1
            }
        }

        let totalHeight:CGFloat = CGFloat(row) * kItemHeight + self.layout.sectionInset.top + self.layout.sectionInset.bottom
        self.model.height = totalHeight + self.cellHeight * 0.5 + self.iconBtnImageHeight * 0.5
        return totalHeight
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let selectedItem:JSBoxItem = self.model.items[indexPath.item]
        selectedItem.selected = !selectedItem.selected
        self.collectionView.reloadData()
        if selectedItem.selected {
            
            if self.model.maxValue != 0 && self.selectedItemNames.count >= Int(self.model.maxValue) {
                JSShowMessage.showTipMsg("最多选择3项", time: 0.6)
                selectedItem.selected = !selectedItem.selected
                return
            }
            self.selectedItemNames.add(selectedItem.name)
            
        }else if ((self.selectedItemNames.contains(selectedItem.name))){
            self.selectedItemNames.remove(selectedItem.name)
        }
        self.model.text = (self.selectedItemNames.componentsJoined(by: ","))
        
        if self.model.name.contains("货方") {
            self.parentVC.uploadDict[self.model.key!+"KDMessage"] = self.selectedItemNames.contains("揽货") ?"1":"0"
            self.parentVC.uploadDict[self.model.key!+"FCMessage"] = self.selectedItemNames.contains("发车") ?"1":"0"
            self.parentVC.uploadDict[self.model.key!+"DZMessage"] = self.selectedItemNames.contains("到站") ?"1":"0"
            self.parentVC.uploadDict[self.model.key!+"QSMessage"] = self.selectedItemNames.contains("签收") ?"1":"0"
        }else
        {
            self.parentVC.uploadDict[self.model.key!] = self.selectedItemNames.componentsJoined(by: ",")
        }
        
        

    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
